<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <ul>
            <li v-for="(item, index) in shopList" :key="item.id">
                <h3>
                    {{ item.title }}
                </h3>
                <div>
                    <input v-model="item.check" type="checkbox">

                    <button @click="delCount(index)">--</button>
                    <input style="width: 20px" v-model="item.count" type="text">
                    <button @click="addCount(index)">++</button>
                </div>
            </li>
        </ul>
        <h3>
            总数量：{{ allCount }}
        </h3>
        <h3>
            总价格：{{ allPrice.toFixed(2) }}
        </h3>
    </div>


    <script src="./js/vue.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.25.0/axios.js"></script>
    <!-- axios -->
    <script src="https://cdn.bootcdn.net/ajax/libs/Mock.js/1.0.1-beta3/mock-min.js"></script>
    <!-- Mock -->
    <script>
        // Mock.setup({
        //     timeout: "1000-5000"
        // })

        Mock.mock('/getList', 'get', [
            {
                id: 1,
                title: "商品1",
                count: 1,
                check: false,
                price: 10
            },
            {
                id: 2,
                title: "商品2",
                count: 1,
                check: true,
                price: 15
            },
            {
                id: 3,
                title: "商品3",
                count: 1,
                check: false,
                price: 20
            }
        ])



        new Vue({
            el: "#app",
            data: {
                shopList: []
            },
            computed: {
                allCount() {
                    return this.shopList.reduce((next, prev) => {
                        if (prev.check) {
                            return next + prev.count
                        } else {
                            return next
                        }
                    }, 0)
                },
                allPrice() {
                    return this.shopList.reduce((next, prev) => {
                        if (prev.check) {
                            return next + prev.count * prev.price
                        } else {
                            return next
                        }
                    }, 0)
                }
            },
            created() {
                axios.get('/getList').then(res => {
                    if (res.status === 200) {
                        this.shopList = res.data
                    }
                })
            },
            methods: {
                addCount(index) {
                    this.shopList[index].count += 1
                },
                delCount(index) {
                    if (this.shopList[index].count > 1) {
                        this.shopList[index].count -= 1
                    } else {
                        this.shopList.splice(index, 1)
                    }

                }
            }
        })

        // let arr = [1, 2, 3, 4, 5]
        // // next (上一次结果的值)
        // // prev 当前循环的数据
        // // 1  0  =  1
        // // 1  2  =  3
        // // 3  3  =  6
        // // 6  4  =  10
        // // 10 5  =  15
        // let all = arr.reduce((next, prev) => {
        //     return next + prev
        // }, 0)


        let arr = [
            {
                id: 1,
                title: "商品1",
                count: 1,
                check: true
            },
            {
                id: 2,
                title: "商品2",
                count: 5,
                check: true
            },
            {
                id: 3,
                title: "商品3",
                count: 7,
                check: false
            }
        ]
        // 0 + 1  =  1
        // 1 + 5  =  6
        // 6

        let all = arr.reduce((next, prev) => {
            if (prev.check) {
                return next + prev.count
            } else {
                return next
            }
        }, 0)

        // console.log(all)
    </script>
</body>

</html>